capt prog drop diffTest3
program diffTest3, eclass
  syntax varlist [if] [in], by(varname) [strata(varlist) ///
         usestrat(integer 0) casewise(integer 1)] [ * ] 
  marksample touse
  markout `touse' `by'
  tempname N N_1 N_2 sd_1 sd_2 mu_1 mu_2 d d_se d_t d_p
  foreach var of local varlist {
      if `casewise' == 0 {
          qui ttest `var' if `touse', by(`by') `options'
      }
      else {
           ttest `var', by(`by') `options'
      }        
      local N_T = r(N_1) + r(N_2)
      mat `N'   = nullmat(`N'), `N_T'
      mat `N_1' = nullmat(`N_1'), r(N_2)
      mat `N_2' = nullmat(`N_2'), r(N_1)
      mat `mu_1' = nullmat(`mu_1'), r(mu_2)
      mat `mu_2' = nullmat(`mu_2'), r(mu_1)
      mat `sd_1' = nullmat(`sd_1'), r(sd_2)
      mat `sd_2' = nullmat(`sd_2'), r(sd_1)
  
      if `usestrat' {
          qui xi: reg `var' `by' i.`strata', vce(robust)
      }
      else {
          qui reg `var' `by', vce(robust)
      }
      mat `d'    = nullmat(`d'   ), _b[`by']
      mat `d_se' = nullmat(`d_se'), _se[`by']
      mat `d_t'  = nullmat(`d_t' ), (_b[`by']/_se[`by'])
      mat `d_p'  = nullmat(`d_p' ), (2*ttail( e(df_r), abs(_b[`by']/_se[`by'])))
  }
  foreach mat in mu_1 mu_2 sd_1 sd_2 d d_se d_t d_p N N_1 N_2 {
      mat coln ``mat'' = `varlist'
  }
  tempname b V
  mat `b' = `mu_1'*0
  mat `V' = `b''*`b'
  eret post `b' `V'
  eret local cmd "meanDiffTable"
  foreach mat in mu_1 mu_2 sd_1 sd_2 d d_se d_t d_p N N_1 N_2 {
      eret mat `mat' = ``mat''
  }

end
